home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xfig.idb / usr / freeware / src / xfig / transfig.3.1.2 / fig2dev / fig2dev.c.z / fig2dev.c
Encoding:
C/C++ Source or Header  |  1997-09-09  |  7.3 KB  |  291 lines

  1. /*
  2.  * TransFig: Facility for Translating Fig code
  3.  * Copyright (c) 1985 Supoj Sutantavibul
  4.  * Copyright (c) 1991 Micah Beck
  5.  *
  6.  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  7.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  8.  * EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  9.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  10.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  11.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  12.  * PERFORMANCE OF THIS SOFTWARE.
  13.  *
  14.  * The X Consortium, and any party obtaining a copy of these files from
  15.  * the X Consortium, directly or indirectly, is granted, free of charge, a
  16.  * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
  17.  * nonexclusive right and license to deal in this software and
  18.  * documentation files (the "Software"), including without limitation the
  19.  * rights to use, copy, modify, merge, publish, distribute, sublicense,
  20.  * and/or sell copies of the Software, and to permit persons who receive
  21.  * copies from any such party to do so, with the only requirement being
  22.  * that this copyright notice remain intact.  This license includes without
  23.  * limitation a license to do the foregoing actions under any patents of
  24.  * the party supplying this software to the X Consortium.
  25.  */
  26.  
  27. /* 
  28.  *    Fig2dev : General Fig code translation program
  29.  *
  30. */
  31. #if defined(hpux) || defined(SYSV) || defined(SVR4)
  32. #include <sys/types.h>
  33. #endif
  34. #include <sys/file.h>
  35. #include <stdio.h>
  36. #include <ctype.h>
  37. #include "patchlevel.h"
  38. #include "fig2dev.h"
  39. #include "object.h"
  40. #include "drivers.h"
  41.  
  42. extern int fig_getopt();
  43. extern char *optarg;
  44. extern int optind;
  45.  
  46. struct driver *dev = NULL;
  47.  
  48. char        Usage[] = "Usage: %s [-L language] [-f font] [-s size] [-m scale] [input [output]]\n";
  49. char        Err_badarg[] = "Argument -%c unknown to %s driver.";
  50. char        Err_incomp[] = "Incomplete %s object at line %d.";
  51. char        Err_mem[] = "Running out of memory.";
  52.  
  53. char        *prog;
  54. char        *from = NULL, *to = NULL;
  55. char        *name = NULL;
  56. int        font_size = 0;
  57. double        mag = 1.0;
  58. FILE        *tfp = NULL;
  59. int        llx = 0, lly = 0, urx = 0, ury = 0;
  60. int        landscape;
  61. int        center;
  62. int        orientspec=0;        /* set it the user-specifies the orientation */
  63. Boolean        pats_used, pattern_used[NUMPATTERNS];
  64.  
  65. struct obj_rec {
  66.     void (*gendev)();
  67.     char *obj;
  68.     int depth;
  69. };
  70.  
  71. put_msg(format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
  72. char   *format, *arg1, *arg2, *arg3, *arg4, *arg5, *arg6, *arg7, *arg8;
  73. {
  74.     fprintf(stderr, format, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  75.     fprintf(stderr, "\n");
  76.     }
  77.  
  78. get_args(argc, argv)
  79. int     argc;
  80. char    *argv[];
  81. {
  82.       int    c, i;
  83.     double    atof();
  84.  
  85.     prog = *argv;
  86. /* add :? */
  87.     /* sum of all arguments */
  88.     while ((c = fig_getopt(argc, argv, "acC:d:ef:l:L:Mm:n:Pp:s:S:t:vVx:X:y:Y:wWz:?")) != EOF) {
  89.  
  90.       /* generic option handling */
  91.       switch (c) {
  92.  
  93.         case 'V': 
  94.             fprintf(stderr, "fig2dev Version %s Patchlevel %s\n",
  95.                             VERSION, PATCHLEVEL);
  96.             exit(0);
  97.             break;
  98.  
  99.         case 'L':            /* set output language */
  100.             for (i=0; *drivers[i].name; i++) 
  101.             if (!strcmp(optarg, drivers[i].name))
  102.                 dev = drivers[i].dev;
  103.             if (!dev) {
  104.             fprintf(stderr,
  105.                 "Unknown graphics language %s\n", optarg);
  106.             fprintf(stderr,"Known languages are:\n");
  107.             /* display available languages - 23/01/90 */
  108.             for (i=0; *drivers[i].name; i++)
  109.                 fprintf(stderr,"%s ",drivers[i].name);
  110.             fprintf(stderr,"\n");
  111.             exit(1);
  112.             }
  113.             break;
  114.  
  115.         case 's':            /* set default font size */
  116.             font_size = atoi(optarg);
  117.             /* max size is checked in respective drivers */
  118.             if (font_size <= 0)
  119.             font_size = DEFAULT_FONT_SIZE;
  120.             font_size = round(font_size * mag);
  121.             break;
  122.  
  123.         case 'm':            /* set magnification */
  124.             mag = atof(optarg);
  125.             break;
  126.  
  127.         case '?':            /* usage         */
  128.             fprintf(stderr,Usage,prog);
  129.             exit(1);
  130.         }
  131.  
  132.         /* pass options through to driver */
  133.         if (!dev) {
  134.         fprintf(stderr, "No graphics language specified.\n");
  135.         exit(1);
  136.         }
  137.         dev->option(c, optarg);
  138.           }
  139.           if (!dev) {
  140.         fprintf(stderr, "No graphics language specified.\n");
  141.         exit(1);
  142.           }
  143.  
  144.     if (optind < argc)
  145.         from = argv[optind++];  /*  from file  */
  146.     if (optind < argc)
  147.         to   = argv[optind];  /*  to file    */
  148. }
  149.  
  150. main(argc, argv)
  151. int     argc;
  152. char    *argv[];
  153. {
  154.     F_compound    objects;
  155.     int        status;
  156.  
  157.     get_args(argc, argv);
  158.  
  159.     if (from)
  160.         status = read_fig(from, &objects);
  161.     else    /* read from stdin */
  162.         status = readfp_fig(stdin, &objects);
  163.  
  164.     if (status != 0) {
  165.         if (from) read_fail_message(from, status);
  166.         exit(1);
  167.         }
  168.  
  169.     if (to == NULL)
  170.         tfp = stdout;
  171.     else if ((tfp = fopen(to, "w")) == NULL) {
  172.         fprintf(stderr, "Couldn't open %s", to);
  173.         fprintf(stderr, Usage, prog);
  174.         exit(1);
  175.         }
  176.  
  177.     gendev_objects(&objects, dev);
  178.     if (tfp != stdout) (void)fclose(tfp);
  179.     exit(0);
  180.     }
  181.  
  182. /* count primitive objects & create pointer array */
  183. static int compound_dump(com, array, count, dev)
  184. F_compound *com;
  185. struct obj_rec *array;
  186. int count;
  187. struct driver *dev;
  188. {
  189.       F_arc        *a;
  190.     F_compound    *c;
  191.     F_ellipse    *e;
  192.     F_line        *l;
  193.     F_spline    *s;
  194.     F_text        *t;
  195.  
  196.     for (c = com->compounds; c != NULL; c = c->next)
  197.       count = compound_dump(c, array, count, dev);
  198.     for (a = com->arcs; a != NULL; a = a->next) {
  199.       if (array) {
  200.         array[count].gendev = dev->arc;
  201.         array[count].obj = (char *)a;
  202.         array[count].depth = a->depth;
  203.       }
  204.       count += 1;
  205.     }
  206.     for (e = com->ellipses; e != NULL; e = e->next) {
  207.       if (array) {
  208.         array[count].gendev = dev->ellipse;
  209.         array[count].obj = (char *)e;
  210.         array[count].depth = e->depth;
  211.       }
  212.       count += 1;
  213.     }
  214.     for (l = com->lines; l != NULL; l = l->next) {
  215.       if (array) {
  216.         array[count].gendev = dev->line;
  217.         array[count].obj = (char *)l;
  218.         array[count].depth = l->depth;
  219.       }
  220.       count += 1;
  221.     }
  222.     for (s = com->splines; s != NULL; s = s->next) {
  223.       if (array) {
  224.         array[count].gendev = dev->spline;
  225.         array[count].obj = (char *)s;
  226.         array[count].depth = s->depth;
  227.       }
  228.       count += 1;
  229.     }
  230.     for (t = com->texts; t != NULL; t = t->next) {
  231.       if (array) {
  232.         array[count].gendev = dev->text;
  233.         array[count].obj = (char *)t;
  234.         array[count].depth = t->depth;
  235.       }
  236.       count += 1;
  237.     }
  238.     return count;
  239. }
  240.  
  241. gendev_objects(objects, dev)
  242. F_compound    *objects;
  243. struct driver *dev;
  244. {
  245.     int obj_count, rec_comp();
  246.     struct obj_rec *rec_array, *r; 
  247.  
  248.     if (0 == (double)objects->nwcorner.x) {
  249.         fprintf(stderr, "Resolution is zero!! default to 80 ppi\n");
  250.         objects->nwcorner.x = 80;
  251.         }
  252.     if (objects->nwcorner.y != 1 && objects->nwcorner.y != 2) {
  253.         fprintf(stderr, "Wrong coordinate system; cannot continue\n");
  254.         return;
  255.         }
  256.  
  257.     /* Compute bounding box of objects, supressing texts if indicated */
  258.     compound_bound(objects, &llx, &lly, &urx, &ury, dev->text_include);
  259.  
  260.     /* dump object pointers to an array */
  261.     obj_count = compound_dump(objects, 0, 0, dev);
  262.     if (!obj_count) {
  263.         fprintf(stderr, "No object");
  264.         return;
  265.         }
  266.     rec_array = (struct obj_rec *)malloc(obj_count*sizeof(struct obj_rec));
  267.     (void)compound_dump(objects, rec_array, 0, dev);
  268.  
  269.     /* sort object array by depth */
  270.     qsort(rec_array, obj_count, sizeof(struct obj_rec), rec_comp);
  271.  
  272.     /* generate header */
  273.     (*dev->start)(objects);
  274.  
  275.     /* generate objects in sorted order */
  276.     for (r = rec_array; r<rec_array+obj_count; r++)
  277.         (*(r->gendev))(r->obj);
  278.  
  279.     /* generate trailer */
  280.     (*dev->end)();
  281. }
  282.  
  283. int rec_comp(r1, r2)
  284. struct obj_rec *r1, *r2;
  285. {
  286.     return (r2->depth - r1->depth);
  287. }
  288.  
  289. /* null operation */
  290. void gendev_null() {;}
  291.